package com.xnew.holiday.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.holiday.entity.HolidayManage;
import com.xnew.holiday.mapper.HolidayManageMapper;
import com.xnew.holiday.service.HolidayManageService;
import com.xnew.utils.DataInput;
import com.xnew.utils.DataOutput;
import com.xnew.utils.IdUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class HolidayManageServiceImpl implements HolidayManageService {

    @Resource
    private HolidayManageMapper holidayManageMapper;

    /**
     * 节假日列表
     * @param input
     * @param userInfo
     * @return
     */
    @Override
    public List<HolidayManage> holidayManageList(DataInput input, SysUserInfo userInfo) {
        Map param = input.getParam();
        if (input.getPageNum() != null && input.getPageSize() != null){
            PageHelper.startPage(input.getPageNum(), input.getPageSize());
        }
        return holidayManageMapper.holidayManageList(param);
    }

    /**
     * 节假日查询
     * @param hmId
     * @return
     */
    @Override
    public HolidayManage queryHolidayManage(String hmId) {
        return holidayManageMapper.selectById(hmId);
    }

    /**
     * 年份是否存在
     * @param holidayManage
     * @param userInfo
     * @return
     */
    @Override
    public DataOutput whetherYearExists(HolidayManage holidayManage, SysUserInfo userInfo) {
        DataOutput out = DataOutput.success();
        Map<String, Object> selectMap = new HashMap<>();
        selectMap.put("hm_year", holidayManage.getHmYear());
        List<HolidayManage> holidayManageList = holidayManageMapper.selectByMap(selectMap);
        if (holidayManageList != null && holidayManageList.size() > 0) {
            out.setCode(-9001);
            out.setMsg("该年的节假日已存在,请重新输入！");
            return out;
        }
//        holidayManage.setHmAddDate(DateUtil.now());
//        holidayManage.setHmAddUser(userInfo.getUserInfoNo());
//        holidayManage.setHmId(IdUtils.getIncreaseIdByNanoTime());
//        holidayManageMapper.insert(holidayManage);
        return out;
    }

    /**
     * 新增节假日信息
     * @param holidayManage
     * @param userInfo
     */
    @Override
    public void holidayManageAdd(HolidayManage holidayManage, SysUserInfo userInfo) {
        holidayManage.setHmAddDate(DateUtil.now());
        holidayManage.setHmAddUser(userInfo.getUserInfoNo());
        holidayManage.setHmId(IdUtils.getIncreaseIdByNanoTime());
        holidayManageMapper.insert(holidayManage);
    }

    /**
     * 修改节假日信息
     * @param holidayManage
     * @param userInfo
     */
    @Override
    public void holidayManageUpdate(HolidayManage holidayManage, SysUserInfo userInfo) {
        holidayManage.setHmUpdateUser(userInfo.getUserInfoNo());
        holidayManage.setHmUpdateDate(DateUtil.now());
        holidayManageMapper.updateById(holidayManage);
    }

    /**
     * 节假日删除
     * @param hmId
     */
    @Override
    public void holidayManageDelete(String hmId) {
        holidayManageMapper.deleteById(hmId);
    }
}
